package com.bdqn.oa.dao.impl;
import com.bdqn.oa.dao.AttendanceDao;
import com.bdqn.oa.entity.Attendance;
import com.bdqn.oa.utils.DruidUtils;
import com.bdqn.oa.utils.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhowei
 * @date 2022/7/20 15:05
 */
public class AttendanceDaoImpl implements AttendanceDao {
    private QueryRunner queryRunner = new QueryRunner();

    /**
     * 上班打卡
     * @param attendance
     * @return
     * @throws SQLException
     */
    @Override
    public int insertMorningAtt(Attendance attendance) throws SQLException {
        String sql="insert into attendance (date,employee_id,type,start_time,end_time,total_time)" +
                "value(?,?,?,?,?,?);";
        return  queryRunner.update(DruidUtils.getConnection(),sql,
                attendance.getDate(),attendance.getEmployeeId(),attendance.getType(),attendance.getStartTime(),attendance.getEndTime(),
                attendance.getTotalTime());
    }

    /**
     * 下班打卡，通过员工id查找上班打卡数据，新增下班打卡时间
     *
     * @param
     * @return
     */
    @Override
    public int insertAfternoonTimeAtt(Attendance attendance) throws SQLException {
        String sql = "update attendance set end_time=?,type=?,total_time=? where employee_id = ? and date=?";
        return queryRunner.update(DruidUtils.getConnection(),sql,attendance.getEndTime(),attendance.getType(),attendance.getTotalTime(),attendance.getEmployeeId(),attendance.getDate());
    }

    @Override
    public long countById(Integer id) throws SQLException {
        List<Attendance> list = new ArrayList<Attendance>();
        list=queryRunner.query(DruidUtils.getConnection(),"select * from attendance where employee_id=?",new BeanListHandler<Attendance>(Attendance.class),id);
        return list.size();
    }

    @Override
    public List<Attendance> getAttendByEid(Integer id) throws SQLException {
        String sql="select * from attendance where employee_id=?";
        return queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Attendance>(Attendance.class),id);
    }

    @Override
    public Page<Attendance> getAttendByEidOfPage(Integer id, long pageNumber, long pageSize) throws SQLException {
        Page attendacePage = new Page();
        attendacePage.setPageNumber(pageNumber);
        attendacePage.setPageSize(pageSize);
        attendacePage.setData(queryRunner.query(DruidUtils.getConnection(),"select * from attendance where employee_id=? limit ?,?",new BeanListHandler<Attendance>(Attendance.class),
                id,(pageNumber-1)*pageSize,pageSize));
        return attendacePage;
    }

    /**
     * 获取某天内员工打卡总数
     *
     * @param date
     * @param id
     * @return
     */
    @Override
    public int countAttByDate(LocalDateTime date, Integer id) throws SQLException {
        List<Attendance> list = queryRunner.query(DruidUtils.getConnection(),"select * from attendance where date=? and employee_id=?",new BeanListHandler<Attendance>(Attendance.class),date,id);
        return list.size();
    }

    /**
     * 查看某一员工的某一天的打卡详情
     * @param id
     * @param date
     * @return
     */
    @Override
    public Attendance getAttendByEidByDate(Integer id, LocalDateTime date) throws SQLException {
        String sql="select * from attendance where employee_id=? and date=?";
        return queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Attendance>(Attendance.class),id,date);
    }

    /**
     * 某一时间段的打卡详情(支持分页)
     * @param id
     * @param start
     * @param end
     * @return
     */
    @Override
    public Page<Attendance> getAttendByEidOfDate(Integer id, LocalDateTime start, LocalDateTime end,long pageNumber,long pageSize) throws SQLException {
        Page attendacePage = new Page();
        attendacePage.setPageNumber(pageNumber);
        attendacePage.setPageSize(pageSize);
        attendacePage.setData(queryRunner.query(DruidUtils.getConnection(),"select * from attendance where employee_id=? and date between ? and ? limit ?,?",new BeanListHandler<Attendance>(Attendance.class),
                id,start,end,(pageNumber-1)*pageSize,pageSize));
        return attendacePage;
    }
}
